package com.major;

import java.util.HashMap;
import java.util.Map;

public class Request {
    private String type;
    private String url;
    private String protocol;

    private String ContentType;

    private Map<String,String> headers = new HashMap<>();

    private Map<String,String> attributes = new HashMap<>();

    /**
     * 建立请求对象
     * @param requestStr
     * @return
     */
    public static Request buildRequest(String requestStr){

        Request request = new Request();
        String[] split = requestStr.split("\r\n\r\n");

        // 请求行 & 请求头
        String[] lineAndHeader = split[0].split("\r\n");
        String[] lines = lineAndHeader[0].split(" ");
        request.setType(lines[0]);
        request.setUrl(lines[1]);
        request.setProtocol(lines[2]);

        for (int i = 1; i < lineAndHeader.length; i++) {
            String[] header = lineAndHeader[i].split(": ");
            request.getHeaders().put(header[0].trim().toLowerCase(),header[1].trim());
        }

        request.setContentType(request.getHeaders().get("content-type"));

        // 处理请求体
        if (split.length == 2){

        }

        return request;
    }
    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getProtocol() {
        return protocol;
    }

    public void setProtocol(String protocol) {
        this.protocol = protocol;
    }

    public String getContentType() {
        return ContentType;
    }

    public void setContentType(String contentType) {
        ContentType = contentType;
    }

    public Map<String, String> getHeaders() {
        return headers;
    }

    public void setHeaders(Map<String, String> headers) {
        this.headers = headers;
    }

    public Map<String, String> getAttributes() {
        return attributes;
    }

    public void setAttributes(Map<String, String> attributes) {
        this.attributes = attributes;
    }

    @Override
    public String toString() {
        return "Request{" +
                "type='" + type + '\'' +
                ", url='" + url + '\'' +
                ", protocol='" + protocol + '\'' +
                ", ContentType='" + ContentType + '\'' +
                ", headers=" + headers +
                ", attributes=" + attributes +
                '}';
    }

    public Request() {
    }

    public Request(String type, String url, String protocol, String contentType, Map<String, String> headers, Map<String, String> attributes) {
        this.type = type;
        this.url = url;
        this.protocol = protocol;
        ContentType = contentType;
        this.headers = headers;
        this.attributes = attributes;
    }
}
